using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ClientGUI.Modules.Drawing
{

    /// <summary>
    /// Enum to mark the status of a EditableTextDisplayArea
    /// </summary>
    public enum EditableTextResult { 
        /// <summary>
        /// Incomplete state (eg no button has been pressed).
        /// </summary>
        INCOMPLETE,
        /// <summary>
        /// User canceled
        /// </summary>
        CANCEL, 
        /// <summary>
        /// The user finialized the text edits
        /// </summary>
        FINAL }

    /// <summary>
    /// Display a text area that can be edited by the user.  This is accomplished by displaying 
    /// a RichTextBox on the region the text would cover.
    /// </summary>
    public class EditableTextDisplayArea : DisplayArea
    {
        RichTextBox my_txt_area;
        String my_initial_text;

        Button my_btn_final;
        Button my_btn_cancel;

        Rectangle my_region;

        DisplayDocument my_container;

        Module my_backing_module;

        Boolean isDisplayed;

        EditableTextResult result;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="backingModule">The module that is using this DisplayArea to edit text</param>
        /// <param name="host">Container control that displays this module</param>
        /// <param name="text">Initial text</param>
        /// <param name="region">Size to give this text area</param>
        public EditableTextDisplayArea(Module backingModule, DisplayDocument host, String text, Rectangle region)
        {

            my_btn_cancel = new Button();
            my_btn_cancel.Text = "Cancel";
            my_btn_cancel.AutoSize = true;
            my_btn_cancel.Click += new EventHandler(my_btn_cancel_Click);
            my_btn_cancel.FlatStyle = FlatStyle.Flat;

            my_btn_final = new Button();
            my_btn_final.Text = "Finalize Text";
            my_btn_final.AutoSize = true;
            my_btn_final.Click += new EventHandler(my_btn_final_Click);
            my_btn_final.FlatStyle = FlatStyle.Flat;
            //TODO: Make buttons do stuff

            my_txt_area = new RichTextBox();
            my_txt_area.Multiline = true;
            my_txt_area.Text = text;
            my_txt_area.BorderStyle = BorderStyle.None;
            my_txt_area.Font = new Font("Sans Serf", 12.0f);
            my_txt_area.Focus();
                

            my_initial_text = text;
            my_region = region;

            my_container = host;

            my_backing_module = backingModule;
            isDisplayed = false;

            result = EditableTextResult.INCOMPLETE;
        }

        /// <summary>
        /// The user decided to not commit the text changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void my_btn_cancel_Click(object sender, EventArgs e)
        {
            my_container.Controls.Remove(my_btn_cancel);
            my_container.Controls.Remove(my_btn_final);
            my_container.Controls.Remove(my_txt_area);
            my_btn_final.Hide();
            my_btn_cancel.Hide();
            my_txt_area.Hide();

            my_txt_area.Text = my_initial_text;
            result = EditableTextResult.CANCEL;
            my_container.UnselectCurrent();
        }

        /// <summary>
        /// The user is commiting the text changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void my_btn_final_Click(object sender, EventArgs e)
        {
            my_container.Controls.Remove(my_btn_cancel);
            my_container.Controls.Remove(my_btn_final);
            my_container.Controls.Remove(my_txt_area);
            my_btn_final.Hide();
            my_btn_cancel.Hide();
            my_txt_area.Hide();

            result = EditableTextResult.FINAL;
            my_container.UnselectCurrent();
        }


        /// <summary>
        /// Moves the controls around and makes them visible if they are not.
        /// </summary>
        /// <param name="displayArea"></param>
        /// <param name="cr"></param>
        /// <param name="offset"></param>
        public override void Composite(Graphics displayArea, Rectangle cr, Point offset)
        {
            my_txt_area.Top = offset.Y;
            my_txt_area.Left = offset.X;
            my_txt_area.Width = my_region.Width;
            my_txt_area.Height = my_region.Height - my_btn_cancel.Height - 2;

            my_btn_final.Top = offset.Y + my_region.Height - my_btn_final.Height;
            my_btn_final.Left = offset.X + my_region.Width - my_btn_final.Width;

            my_btn_cancel.Top = offset.Y + my_region.Height - my_btn_cancel.Height;
            
            //get 5 pixels between buttons
            my_btn_cancel.Left = offset.X + my_region.Width - my_btn_cancel.Width 
                                 - my_btn_final.Width - 5;


            if (!isDisplayed)
            {
                my_container.Controls.Add(my_btn_cancel);
                my_container.Controls.Add(my_btn_final);
                my_container.Controls.Add(my_txt_area);
                my_btn_final.Show();
                my_btn_cancel.Show();
                my_txt_area.Show();
                my_txt_area.Focus();
            }

            isDisplayed = true;
        }

        /// <summary>
        /// Used to update the displayed area of this component.  Useful if the screen resizes
        /// during display.
        /// </summary>
        public Rectangle Region
        {
            get
            {
                return my_region;
            }
            set
            {
                my_region = value;
            }
        }
        
        /// <summary>
        /// Return the current result status of this EditableTextResult
        /// </summary>
        public EditableTextResult Result { get { return result; } }

        /// <summary>
        /// Current text of this module.
        /// </summary>
        public String Text { get { return my_txt_area.Text; } }

        #region IDisposable Members

        /// <summary>
        /// Dispose this control's memory
        /// </summary>
        public override void Dispose()
        {
            my_txt_area.Dispose();
            my_btn_final.Dispose();
            my_btn_cancel.Dispose();
        }

        #endregion
    }
}
